#import "tool_action.h"
#import "tool_point.h"

/*action_call_back
*/
@implementation action_call_back

+ (id)actionWithDuration:(id)Object time:(ccTime)Time callback:(SEL)selector
{
	return [[[self alloc] initWithDuration:Object time:Time callback:selector] autorelease];
}

- (id)initWithDuration:(id)Object time:(ccTime)Time callback:(SEL)selector
{
	if ((self = [super initWithDuration:Time]))
	{
		targetCallBack_ = Object;
		selector_ = selector;
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
	CCAction *copy = [[[self class] allocWithZone:zone] initWithDuration:targetCallBack_ time:[self duration] callback:selector_];
	return copy;
}

- (void)startWithTarget:(CCNode *)aTarget
{
	[super startWithTarget:aTarget];
}

- (void)stop
{
	[super stop];
	[targetCallBack_ performSelector:selector_];
}

@end

/*action_move_in_pixel
*/
@implementation action_move_in_pixel

+ (id)actionWithDuration:(ccTime)Time position:(CGPoint)Position
{
	return [[[self alloc] initWithDuration:Time position:Position] autorelease];
}

- (id)initWithDuration:(ccTime)Time position:(CGPoint)Position
{
	if ((self = [super initWithDuration:Time]))
	{
		endPosition_ = Position;
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
	CCAction *copy = [[[self class] allocWithZone:zone] initWithDuration:[self duration] position:endPosition_];
	return copy;
}

- (void)startWithTarget:(CCNode *)aTarget
{
	[super startWithTarget:aTarget];

	startPosition_ = [(CCNode *) target_ positionInPixels];
	delta_ = ccpSub(endPosition_, startPosition_);
}

- (void)update:(ccTime)Time
{
	[target_ setPositionInPixels:ccp((startPosition_.x + delta_.x * Time), (startPosition_.y + delta_.y * Time))];
}

@end

/*action_move_x_in_pixel
*/
@implementation action_move_x_in_pixel

+ (id)actionWithDuration:(ccTime)Time position:(CGPoint)Position
{
	return [[[self alloc] initWithDuration:Time position:Position] autorelease];
}

- (id)initWithDuration:(ccTime)Time position:(CGPoint)Position
{
	if ((self = [super initWithDuration:Time]))
	{
		endPosition_ = Position;
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
	CCAction *copy = [[[self class] allocWithZone:zone] initWithDuration:[self duration] position:endPosition_];
	return copy;
}

- (void)startWithTarget:(CCNode *)aTarget
{
	[super startWithTarget:aTarget];

	startPosition_ = [(CCNode *) target_ positionInPixels];
	delta_ = ccpSub(endPosition_, startPosition_);
}

- (void)update:(ccTime)Time
{
	CGPoint _Position;

	_Position = [(CCNode *) target_ positionInPixels];
	[target_ setPositionInPixels:ccp(startPosition_.x + delta_.x * Time, _Position.y)];
}

@end

/*action_move_y_in_pixel
*/
@implementation action_move_y_in_pixel

+ (id)actionWithDuration:(ccTime)Time position:(CGPoint)Position
{
	return [[[self alloc] initWithDuration:Time position:Position] autorelease];
}

- (id)initWithDuration:(ccTime)Time position:(CGPoint)Position
{
	if ((self = [super initWithDuration:Time]))
	{
		endPosition_ = Position;
	}
	return self;
}

- (id)copyWithZone:(NSZone *)zone
{
	CCAction *copy = [[[self class] allocWithZone:zone] initWithDuration:[self duration] position:endPosition_];
	return copy;
}

- (void)startWithTarget:(CCNode *)aTarget
{
	[super startWithTarget:aTarget];

	startPosition_ = [(CCNode *) target_ positionInPixels];
	delta_ = ccpSub(endPosition_, startPosition_);
}

- (void)update:(ccTime)Time
{
	CGPoint _Position;

	_Position = [(CCNode *) target_ positionInPixels];
	[target_ setPositionInPixels:ccp(_Position.x, startPosition_.y + delta_.y * Time)];
}

@end

action_call_back *tcCreateActionCallBack(id Object, float Time, SEL Selector)
{
	return [action_call_back actionWithDuration:Object time:Time callback:Selector];
}

action_move_in_pixel *tcCreateActionMove(CGPoint Start, CGPoint End, float Speed)
{
	float _Distance;
	float _Time;

	_Distance = ptPointDistance_CGPoint(Start, End);
	_Time = _Distance / Speed;
	return [action_move_in_pixel actionWithDuration:_Time position:End];
}

CCRepeatForever *tcCreateRepeatAnimate(UIAnimationObject *Object)
{
	CCRepeatForever *_Ret;
	CCAnimate *_Animate;
	CCAnimation *_Animation;

	_Animation = uiCreateAnimationObject(Object);
	if (nil != _Animation)
	{
		_Animate = [CCAnimate actionWithAnimation:_Animation];
		_Ret = [CCRepeatForever actionWithAction:_Animate];
		return _Ret;
	}
	else
	{
		return nil;
	}
}